package com.demo.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Log;

public class MD5Encryptor {

	private static final String TAG = "MD5Encryptor";

	public static String encrypt(String val) {
		String result = null;
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(val.getBytes());
			byte[] m = md5.digest();
			result = getString(m);
		} catch (NoSuchAlgorithmException e) {
			Log.w(TAG, "Md5 encrypt error ,the exception is :" + e.getMessage().toString());
			e.printStackTrace();
		}
		return result;
	}

	private static String getString(byte[] b) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			sb.append(b[i]);
		}
		return sb.toString();
	}
}
